package com.ruoyi.business.service.impl;

import com.ruoyi.bizsys.domain.BankCode;
import com.ruoyi.bizsys.service.IBankCodeService;
import com.ruoyi.business.service.IBankListService;
import com.ruoyi.bizsys.utils.BizConstants;
import com.ruoyi.common.utils.Func;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cyh
 */
@Service
@AllArgsConstructor
public class BankListServiceImpl implements IBankListService {

    private IBankCodeService bankCodeService;


    @Override
    public BankCode getBankCodeByPayStackCode(String payStackCode) {
        BankCode bankCode = new BankCode();
        bankCode.setPaystackBankCode(payStackCode);
        List<BankCode> bankCodes = bankCodeService.selectBankCodeList(bankCode);
        return bankCodes.size() > 0 ? bankCodes.get(0) : null;
    }

    @Override
    public List<BankCode> getBankListSorted() {
        BankCode bankCode = new BankCode();
        bankCode.setState(BizConstants.BIZ_STATUS_1);
        List<BankCode> bankCodes = bankCodeService.selectBankCodeList(bankCode);
        return bankCodes.stream().sorted(Comparator.comparing(BankCode::getSort)).collect(Collectors.toList());
    }

    @Override
    public BankCode getBankCode(BankCode bankCode) {
        List<BankCode> bankLists = bankCodeService.selectBankCodeList(bankCode);
        if (bankLists.size() > 0){
            return bankLists.get(0);
        }
        return null;
    }

    @Override
    public String bankCodeConversion(String bankCode) {
        //OkraId 长度大于7
        if (Func.isNotEmpty(bankCode) && bankCode.length() > 7) {
            BankCode bankCodeQ = new BankCode();
            bankCodeQ.setOkraBankId(bankCode);
            List<BankCode> bankCodes = bankCodeService.selectBankCodeList(bankCodeQ);
            if (bankCodes.size() > 0) {
                return bankCodes.get(0).getPaystackBankCode();
            }
        }
        return bankCode;
    }



}
